package com.yanfan;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.yanfan.energy.domain.IbmsEnergyDevice;
import com.yanfan.energy.domain.IbmsEnergyDeviceData;
import com.yanfan.energy.service.IIbmsEnergyDeviceService;
import com.yanfan.energy.util.HttpGetUtil;
import com.yanfan.ibms.common.utils.HttpUtil;
import com.yanfan.ibms.common.utils.HttpUtils2;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Main {

    public static void main(String[] args) {

//        HashMap<String, Object> map = new HashMap<>();
//        map.put("stationNo", "stationNo");
//
//        JSONObject jsonObject = HttpUtils2.sendPost("http://10.21.10.52:8005/api/v1/device/function/history-data", null,null);
//        List<IbmsEnergyDevice> list = jsonObject.getList(jsonObject.toJSONString("parkingLotInfo"), IbmsEnergyDevice.class);

        String s = "HOHU7XT097620,LFHZXNDUBB620,KFHZXNDUBB620,5JTQGAKXBB620";
        JSONObject jsonObject = HttpGetUtil.sendGetRequest(
                "http://10.21.10.52:8005/api/v1/device/functions?deviceIds="+s,
                "192ded29dc4286f26bc1aa2cffd67d3d01864c08e63366d4d5993b1b83b87112");
        if(jsonObject == null){
            return;
        }
        JSONArray json = jsonObject.getJSONArray("data");

        // 遍历JSON数组
        List<IbmsEnergyDeviceData> list = new ArrayList<>() ;
        for (int i = 0; i < json.size(); i++) {
            JSONObject deviceObj = json.getJSONObject(i);
            String deviceId = deviceObj.getString("deviceId");
            JSONArray functions = deviceObj.getJSONArray("functions");
            for (int j = 0; j < functions.size(); j++) {
                JSONObject functionObj = functions.getJSONObject(j);
                String identifier = functionObj.getString("identifier");
                String value = functionObj.getString("value");
                String time = functionObj.getString("time");
//                    System.out.println("Device ID: " + deviceId + ", Identifier: " + identifier + ", Value: " + value + ", Time: " + time);
                if ("11X".equals(functionObj.getString("identifier"))) {
                    IbmsEnergyDeviceData data = new IbmsEnergyDeviceData();
                    data.setId(deviceId);
                    data.setIdentifier("11X");
                    data.setValue(value);
                    data.setTime(functionObj.getString("time"));
                    list.add(data);
//                    break;
                }else if("12L".equals(functionObj.getString("identifier"))){
                    IbmsEnergyDeviceData data = new IbmsEnergyDeviceData();
                    data.setId(deviceId);
                    data.setIdentifier("12L");
                    data.setValue(value);
                    data.setTime(functionObj.getString("time"));
                    list.add(data);
//                    break;
                }else if("12N".equals(functionObj.getString("identifier"))){
                    IbmsEnergyDeviceData data = new IbmsEnergyDeviceData();
                    data.setId(deviceId);
                    data.setIdentifier("12N");
                    data.setValue(value);
                    data.setTime(functionObj.getString("time"));
                    list.add(data);
//                    break;
                }else if("12O".equals(functionObj.getString("identifier"))){
                    IbmsEnergyDeviceData data = new IbmsEnergyDeviceData();
                    data.setId(deviceId);
                    data.setIdentifier("12O");
                    data.setValue(value);
                    data.setTime(functionObj.getString("time"));
                    list.add(data);
//                    break;
                }
            }
        }
        System.out.println(list);
    }
}
